1592D - Hemose in ICPC - CodeForces Solution


binary search dfs and similar implementation interactive math number theory trees *2300

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define re register
#define pii pair<int,int>
#define fi first
#define se second
#define N 502000

using namespace std;
struct edge{
    int to,next;
}e[N<<1];
const int mo=1000000007;
inline int read(){
    int x=0,w=0;char ch=getchar();
    while (!isdigit(ch))w|=ch=='-',ch=getchar();
    while (isdigit(ch))x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
    return w?-x:x;
}
int n,head[N],cnt,c[N],tot;
pii ed[N];
void add(int u,int v){
    e[++cnt].to=v;
    e[cnt].next=head[u];
    head[u]=cnt;
}
void dfs(int u,int fa){
    for (int i=head[u];i;i=e[i].next){
        int v=e[i].to;
        if (v==fa)continue;
        ed[++tot]=(pii){u,v};
        dfs(v,u);
    }
}
int ck(int l,int r){
    int sum=0;
    for (int i=1;i<=n;++i)c[i]=0;
    for (int i=l;i<=r;++i)c[ed[i].fi]=c[ed[i].se]=1;
    for (int i=1;i<=n;++i)sum+=c[i];
    cout<<"? "<<sum;
    for (int i=1;i<=n;++i)if (c[i])cout<<" "<<i;
    cout<<endl;
    int x;cin>>x;
    return x;
}
signed main(){
    n=read();int mx=0;
    for (int i=1;i<n;++i){
        int u=read(),v=read();
        add(u,v);add(v,u);
    }
    dfs(1,0);
    cout<<"? "<<n;
    for (int i=1;i<=n;++i)cout<<" "<<i;cout<<endl;
    cin>>mx;
    int l=1,r=n-1,res;
    while (l<=r){
        int mid=(l+r)>>1;
        if (ck(l,mid)==mx)r=mid-1,res=mid;
        else l=mid+1,res=mid+1;
    }
    cout<<"! "<<ed[res].fi<<" "<<ed[res].se<<endl;
    return 0;
}


Comments

Submit
0 Comments
More Questions

1684A - Digit Minimization
43B - Letter
1017A - The Rank
1698B - Rising Sand
235A - LCM Challenge
1075B - Taxi drivers and Lyft
1562A - The Miracle and the Sleeper
1216A - Prefixes
1490C - Sum of Cubes
868A - Bark to Unlock
873B - Balanced Substring
1401D - Maximum Distributed Tree
1716C - Robot in a Hallway
1688B - Patchouli's Magical Talisman
99A - Help Far Away Kingdom
622B - The Time
1688C - Manipulating History
1169D - Good Triple
1675B - Make It Increasing
588A - Duff and Meat
1541B - Pleasant Pairs
1626B - Minor Reduction
1680A - Minimums and Maximums
1713A - Traveling Salesman Problem
1713B - Optimal Reduction
1710A - Color the Picture
1686B - Odd Subarrays
251A - Points on Line
427C - Checkposts
1159A - A pile of stones